package com.jjr.action;

import java.io.IOException;
import java.util.*;

import net.sf.json.JSONObject;
import tool.ClientTool;
import tool.IdcardValidator;
import tool.JsonTool;
import tool.WechatTool;

import com.base.bean.*;
import com.base.service.*;
import com.base.dao.*;
import com.hf.bean.OauthBean;
import com.jjr.service.JingJiRenService;

/**
 * 配合php 对json的操作 功能： 验证电话号码（格式 重复性） 姓名重复性 身份证号格式验证
 */
public class OpenIdAction extends BaseAction {

	private static String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
	private static String secret = "7ecc2147d30ac6dfb59629a36dc3e5ce";
	private static String appid = "wx2dfe495e90801424";
	
	private static String checkAccessTockenUrl="https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID";

	private String code;
	private String openId,accessToken;

	

	@Override
	public String execute() {
		OauthBean bean = new OauthBean();
		url = url.replace("SECRET", this.secret);
		url = url.replace("CODE", this.code);
		url = url.replace("APPID", this.appid);
		try {
			String result = WechatTool.readUrl(url);
			JSONObject jsonObject = JSONObject.fromObject(result);
			
			bean.setOpenid(jsonObject.getString("openid"));
			bean.setAccess_token(jsonObject.getString("access_token"));
			
			this.result.setTip("获取成功");
			
			this.result.setState(1);
			this.result.setData(JsonTool.obj2json(bean));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.result.setTip("获取识别码失败");
			this.result.setState(-1);
			this.result.setErrCode(-1);
			this.result.setData(JsonTool.obj2json(bean));
			return SUCCESS;
		}
	}

//	// 验证openId accessToken是否有效
//	public String check(){
//		checkAccessTockenUrl = checkAccessTockenUrl.replace("ACCESS_TOKEN", this.accessToken);
//		checkAccessTockenUrl = checkAccessTockenUrl.replace("OPENID", this.openId);
//		String result = WechatTool.readUrl(url);
//		JSONObject jsonObject = JSONObject.fromObject(result);
//		String r = jsonObject.getString("errmsg");
//		if(r.equals("ok")){
//			this.result.setTip("accessToken有效");
//			this.result.setState(1);
//			this.result.setErrCode(0);
//		}else{
//			this.result.setTip("accessToken过期");
//			this.result.setState(-1);
//			this.result.setErrCode(-1);
//		}
//		
//		return SUCCESS;
//	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	
	

}
